package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.forexpose.StatusEnumForExpose;
import com.jilefojing.beans.enums.forexpose.TypeEnumForExpose;
import com.jilefojing.beans.pojos.Expose;
import com.jilefojing.beans.query.ExposeQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IExposeDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IExposeMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月24日 22时00分28秒
 */
@Repository
public class ExposeDao extends BaseDao<Expose> implements IExposeDao {
	@Autowired
	private IExposeMapper exposeMapper;

	@Override
	public Pagination<Expose> getPagination(ExposeQuery exposeQuery, Pagination<Expose> pagination) {
		Example example = new Example(Expose.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(exposeQuery.getIds())) {
			criteria.andIn(Expose.FIELD_ID, exposeQuery.getIds());
		}

		if (Detect.notEmpty(exposeQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemTypeEnum : exposeQuery.getItemTypes()) {
				itemTypeCodes.add(_ItemTypeEnum.getCode());
			}
			criteria.andIn(Expose.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(exposeQuery.getItemIds())) {
			criteria.andIn(Expose.FIELD_ITEMID, exposeQuery.getItemIds());
		}

		if (Detect.notEmpty(exposeQuery.getTypes())) {
			List<Short> typeCodes = new ArrayList<Short>();
			for (TypeEnumForExpose _Type : exposeQuery.getTypes()) {
				typeCodes.add(_Type.getCode());
			}
			criteria.andIn(Expose.FIELD_TYPE, typeCodes);
		}

		if (Detect.notEmpty(exposeQuery.getUserIds())) {
			criteria.andIn(Expose.FIELD_USERID, exposeQuery.getUserIds());
		}

		if (Detect.notEmpty(exposeQuery.getDealUserIds())) {
			criteria.andIn(Expose.FIELD_DEALUSERID, exposeQuery.getDealUserIds());
		}

		if (Detect.notEmpty(exposeQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForExpose _Status : exposeQuery.getStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(Expose.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(exposeQuery.getLike_content())) {
			criteria.andLike(Expose.FIELD_CONTENT, "%" + exposeQuery.getLike_content() + "%");
		}

		if (Detect.notEmpty(exposeQuery.getContent())) {
			criteria.andEqualTo(Expose.FIELD_CONTENT, exposeQuery.getContent());
		}

		if (Detect.notEmpty(exposeQuery.getLike_dealResult())) {
			criteria.andLike(Expose.FIELD_DEALRESULT, "%" + exposeQuery.getLike_dealResult() + "%");
		}

		if (Detect.notEmpty(exposeQuery.getDealResult())) {
			criteria.andEqualTo(Expose.FIELD_DEALRESULT, exposeQuery.getDealResult());
		}

		if (exposeQuery.getEqualDealTime() != null) {
			criteria.andEqualTo(Expose.FIELD_DEALTIME, exposeQuery.getEqualDealTime());
		}

		if (exposeQuery.getEqualAndBeforDealTime() != null) {
			criteria.andGreaterThanOrEqualTo(Expose.FIELD_DEALTIME, exposeQuery.getEqualAndBeforDealTime());
		}

		if (exposeQuery.getEqualAndAfterDealTime() != null) {
			criteria.andLessThanOrEqualTo(Expose.FIELD_DEALTIME, exposeQuery.getEqualAndAfterDealTime());
		}

		if (exposeQuery.getBeforDealTime() != null) {
			criteria.andGreaterThan(Expose.FIELD_DEALTIME, exposeQuery.getBeforDealTime());
		}

		if (exposeQuery.getAfterDealTime() != null) {
			criteria.andLessThan(Expose.FIELD_DEALTIME, exposeQuery.getAfterDealTime());
		}

		if (exposeQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(Expose.FIELD_CREATETIME, exposeQuery.getEqualCreateTime());
		}

		if (exposeQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Expose.FIELD_CREATETIME, exposeQuery.getEqualAndBeforCreateTime());
		}

		if (exposeQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(Expose.FIELD_CREATETIME, exposeQuery.getEqualAndAfterCreateTime());
		}

		if (exposeQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(Expose.FIELD_CREATETIME, exposeQuery.getBeforCreateTime());
		}

		if (exposeQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(Expose.FIELD_CREATETIME, exposeQuery.getAfterCreateTime());
		}

		if (exposeQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(Expose.FIELD_UPDATETIME, exposeQuery.getEqualUpdateTime());
		}

		if (exposeQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Expose.FIELD_UPDATETIME, exposeQuery.getEqualAndBeforUpdateTime());
		}

		if (exposeQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(Expose.FIELD_UPDATETIME, exposeQuery.getEqualAndAfterUpdateTime());
		}

		if (exposeQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(Expose.FIELD_UPDATETIME, exposeQuery.getBeforUpdateTime());
		}

		if (exposeQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(Expose.FIELD_UPDATETIME, exposeQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}
}
